﻿using ArcGIS.Core.Data;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using CCTool.Scripts.Manager;
using CCTool.Scripts.ToolManagers;
using CCTool.Scripts.ToolManagers.Extensions;
using CCTool.Scripts.ToolManagers.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CCTool.Scripts.DataPross.FeatureClasses
{
    /// <summary>
    /// Interaction logic for SortPlus.xaml
    /// </summary>
    public partial class SortPlus : ArcGIS.Desktop.Framework.Controls.ProWindow
    {
        // 定义一个进度框
        private ProcessWindow processwindow = null;
        string tool_name = "排序加强版";

        public SortPlus()
        {
            InitializeComponent();
        }

        private void combox_zd_DropDown(object sender, EventArgs e)
        {
            UITool.AddFeatureLayerAndTableToComboxPlus(combox_zd);
        }

        private void btn_help_Click(object sender, RoutedEventArgs e)
        {
            string url = "";
            UITool.Link2Web(url);
        }

        private async void btn_go_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // 获取数据
                string zd = combox_zd.ComboxText();
                string fd = combox_field.ComboxText();

                string zdPath = zd.TargetLayerPath();

                // 判断参数是否选择完全
                if (zd == "" || fd == "" )
                {
                    MessageBox.Show("有必选参数为空！！！");
                    return;
                }

                Close();

                // 打开进度框
                ProcessWindow pw = UITool.OpenProcessWindow(processwindow, tool_name);
                pw.AddMessageTitle(tool_name);

                await QueuedTask.Run(() =>
                {
                    // 获取指定的字段
                    Field field = GisTool.GetFieldFromString(zd, fd);

                    string tem_fc = $@"{Project.Current.DefaultGeodatabasePath}\sort_{zd}";

                    // 如果是字符类型，就提取数字再排序
                    if (field.FieldType == FieldType.String)
                    {
                        pw.AddMessageMiddle(20, "字符串类型：提取其中数字，按数字排序");

                        // 提取数字
                        Arcpy.AddField(zd, "numFD", "double");
                        FieldCalTool.GetNumber(zd, fd, "numFD");
                        // 排序
                        Arcpy.Sort(zd, tem_fc, $"numFD ASCENDING", "UR" , true);
                        Arcpy.DeleteField(tem_fc, ["numFD", "ORIG_FID"]);
                        Arcpy.DeleteField(zd, ["numFD"]);
                    }
                    // 如果是其它类型，就正常排序
                    else
                    {
                        pw.AddMessageMiddle(20, "非字符串类型：正常排序");

                        Arcpy.Sort(zd, tem_fc, $"{fd} ASCENDING", "UR", true);
                        Arcpy.DeleteField(tem_fc, ["ORIG_FID"]);
                    }

                });
                pw.AddMessageEnd();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + ee.StackTrace);
                return;
            }
        }

        private void combox_field_DropDown(object sender, EventArgs e)
        {
            UITool.AddFieldsToComboxPlus(combox_zd.ComboxText(), combox_field);
        }
    }
}
